﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Common.Models;

using Module.Core.Common;
using Module.Core.EntitiesInfo.TablesInfo;



namespace Web.Cms.City
{
    public partial class lstCity : AdminUserControl
    {
        #region Khai báo biến
        private int totalRecords;
        private int cPage = 1;
        string cityname;
        private bool isTotalRow;
        private int beginRow;
        private int endRow;
        private string orderByColumn;
        private string cityName;
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                base.OnPreRender(e);

                if (Request.QueryString["cityName"] == "" || Request.QueryString["cityName"] == null)
                {
                    txtKeysearch.Text = "";
                }
                else
                {
                    txtKeysearch.Text = Request.QueryString["cityName"];
                    cityName = txtKeysearch.Text;
                }
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion


           #region LoadData

        private void LoadData()
        {
            totalRecords = CityBL.SelectCount(cityName, isTotalRow);   
            if (Request.QueryString.Get("cpage") != null)
                cPage = ObjConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
            if (cPage == 0) cPage = 1;
            int from = (cPage - 1) * Const.pageSize20 + 1;
            if (from == 0) from = 1;
            int to = (cPage) * Const.pageSize20;

            PagingAjax1.totalRecords = totalRecords;
            PagingAjax1.numPerPage = Const.pageSize20;
            PagingAjax1.queryParam = ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] {"cityName","from", "to", "cPage" },
                                    new string[] {txtKeysearch.Text, from.ToString(), to.ToString(), "" });

            PagingAjax1.defaultQueryParam = ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "from", "to" },
                                    new string[] { from.ToString(), to.ToString() });
            string order = null;
            DataTable dt;
            dt = CityBL.Search(txtKeysearch.Text, 0, 10, orderByColumn, isTotalRow);
            if (dt != null && dt.Rows.Count > 0)
            {

                grdCity.DataSource = dt;
                grdCity.RowDataBound += new GridViewRowEventHandler(grdCity_RowDataBound);
                grdCity.DataBind();

            }
        }
           #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadData();
                  
                }

        }

        protected void grdCity_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Text = (Const.pageSize30 * (cPage - 1) + e.Row.DataItemIndex + 1).ToString();
        }

        protected void grdCity_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            try
            {
                switch (e.CommandName)
                {
                    case "Commanddelete":
                        try
                        {
                            int cityId = Convert.ToInt32(e.CommandArgument);
                            CityBL.Delete(cityId);
                            LoadData();
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {

            Response.Redirect("Default.aspx?ctrlkey=" + CurrentCommand.CtrlKey + "&cityName=" + txtKeysearch.Text  +"&cPage=" + cPage);
        }
    }
}